import {Injectable} from '@angular/core';

import {Constant} from '@core/constant';
import {HttpServer} from '@core/net/http-server';
import {UrlManager} from '@shared/url/url.manager';

import {Observable, Observer, of} from 'rxjs';

import {PageInfo} from '@core/model/pageInfo';
import {Knowledge} from '../model/knowledge';

@Injectable()
export class KnowledgeService {

  constructor(
    private http: HttpServer
  ) {
  }

  private data: Knowledge[] = [];

  private _tid: number;

  private knowledgeList: Knowledge[] = [];

  get knowledges() {
    return this.data;
  }

  get tid(): number {
    return this._tid;
  }

  setKnowledges(knowledges: Knowledge[]): void {
    this.data = knowledges;
  }

  setTid(id: number): void {
    this._tid = id;
  }

  getKnowledgeByPid(pid: number): Observable<any[]> {
    return this.http.get(UrlManager.api.knowledge.getKnowledgeByPid, {
      pid: pid
    });
  }

  getKnowledgeListByPid(pid: number): Observable<any[]> {
    return this.http.get(UrlManager.api.knowledge.getKnowledgeListByPid, {pid: pid});
  }

  getKnowledgeParentByPage(pageNo: number, pageSize: number): Observable<PageInfo<Knowledge>> {
    return this.http.get(UrlManager.api.knowledge.getKnowledgeByPage, {
      pageNo: pageNo,
      pageSize: pageSize
    });
  }

  getKnowledgeByPage(pageNo: number, pageSize: number, knowledgeName?: string): Observable<PageInfo<Knowledge>> {
    if (knowledgeName !== '') {
      return this.getKnowledgeByName(pageNo, pageSize, knowledgeName);
    } else {
      return this.http.get<PageInfo<Knowledge>>(UrlManager.api.knowledge.getKnowledgeByPage, {
        pageNo: pageNo,
        pageSize: pageSize
      });
    }
  }

  getKnowledgeById(id: number): Observable<Knowledge> {
    return this.http.get(UrlManager.api.knowledge.getKnowledgeById, {
      id: id
    });
  }

  getKnowledgeByName(pageNo: number, pageSize: number, KnowledgeName: string): Observable<PageInfo<Knowledge>> {
    return this.http.get(UrlManager.api.knowledge.getKnowledgeByName, {
      pageNo: pageNo,
      pageSize: pageSize,
      knowledgeName: KnowledgeName,
    });
  }


  addKnowledge(item: Knowledge): Observable<Knowledge> {
    return this.http.post(UrlManager.api.knowledge.addKnowledge, item);
  }

  //
  updateKnowledge(item: Knowledge): Observable<boolean> {
    return this.http.post(UrlManager.api.knowledge.updateKnowledgeById, item);
  }

  deltetKnowledge(id: number): Observable<boolean> {
    return this.http.get(UrlManager.api.knowledge.deleteKnowledgeById, {
      id: id
    });
  }
}

